package org.jeecg.modules.order.service.impl;

import org.jeecg.modules.order.entity.BeienProcessRoute;
import org.jeecg.modules.order.entity.BeienProcessRelation;
import org.jeecg.modules.order.mapper.BeienProcessRelationMapper;
import org.jeecg.modules.order.mapper.BeienProcessRouteMapper;
import org.jeecg.modules.order.service.IBeienProcessRouteService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 工艺路线
 * @Author: jeecg-boot
 * @Date:   2024-11-20
 * @Version: V1.0
 */
@Service
public class BeienProcessRouteServiceImpl extends ServiceImpl<BeienProcessRouteMapper, BeienProcessRoute> implements IBeienProcessRouteService {

	@Autowired
	private BeienProcessRouteMapper beienProcessRouteMapper;
	@Autowired
	private BeienProcessRelationMapper beienProcessRelationMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BeienProcessRoute beienProcessRoute, List<BeienProcessRelation> beienProcessRelationList) {
		beienProcessRouteMapper.insert(beienProcessRoute);
		if(beienProcessRelationList!=null && beienProcessRelationList.size()>0) {
			for(BeienProcessRelation entity:beienProcessRelationList) {
				//外键设置
				entity.setRouteId(beienProcessRoute.getId());
				beienProcessRelationMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BeienProcessRoute beienProcessRoute,List<BeienProcessRelation> beienProcessRelationList) {
		beienProcessRouteMapper.updateById(beienProcessRoute);
		
		//1.先删除子表数据
		beienProcessRelationMapper.deleteByMainId(beienProcessRoute.getId());
		
		//2.子表数据重新插入
		if(beienProcessRelationList!=null && beienProcessRelationList.size()>0) {
			for(BeienProcessRelation entity:beienProcessRelationList) {
				//外键设置
				entity.setRouteId(beienProcessRoute.getId());
				beienProcessRelationMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		beienProcessRelationMapper.deleteByMainId(id);
		beienProcessRouteMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			beienProcessRelationMapper.deleteByMainId(id.toString());
			beienProcessRouteMapper.deleteById(id);
		}
	}
	
}
